/*
 * Copyright 2008. Mount Sinai Hospital, Toronto, Canada.
 * 
 * Licensed under the Apache License, Version 2.0. You
 * can find a copy of the license at:
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * IN NO EVENT SHALL MOUNT SINAI HOSPITAL BE LIABLE TO ANY PARTY FOR DIRECT, 
 * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST 
 * PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, 
 * EVEN IF MOUNT SINAI HOSPITAL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 
 * DAMAGE.
 * 
 * MOUNT SINAI HOSPITAL SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE AND 
 * ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". 
 * MOUNT SINAI HOSPITAL HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, 
 * UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 
 */

package com.sinai.mshab.data.gwt;

import com.google.gwt.user.client.ui.ListBox;
import com.sinai.mshab.data.core.DataManager;

/**
 * A concrete implementation of {@link DataManager} for GWT {@link ListBox}
 * widgets. Current this only supports {@code ListBox}es that aren't
 * multiselected. If the {@code ListBox} is multiple select an
 * {@link IllegalStateException} is thrown.
 * 
 * @author Arthur Kalmenson
 */
public class ListBoxDataManager implements DataManager<ListBox, String> {

	/**
	 * The default value that the {@link ListBox}es being processed.
	 */
	private String defaultValue;

	/**
	 * Returns the currently selected value. If the value is equal to
	 * {@code defaultValue} or nothing is selected, null will be returned. If
	 * the given {@link ListBox} has multiple select enabled, we'll throw an
	 * {@link IllegalStateException}.
	 * 
	 * @param widget
	 *            the {@code ListBox} to get the value from.
	 * @return the currently selected value. null if nothing is selected.
	 * @throws IllegalStateException
	 *             if the given {@link ListBox} has multiple select enabled.
	 */
	public String getData(ListBox widget) {

		// we don't support multiple select.
		if (widget.isMultipleSelect()) {
			throw new IllegalStateException("Multiple select ListBoxes "
					+ "are currently not supported");
		}

		String result = null;

		// if an index is selected.
		if (widget.getSelectedIndex() > -1) {

			// and either the default value is null or it's not equal to the
			// selected index, we save the result.
			if (defaultValue == null
					|| !widget.getValue(widget.getSelectedIndex()).equals(
							defaultValue)) {
				result = widget.getValue(widget.getSelectedIndex());
			}
		}

		return result;
	}

	/**
	 * Sets the selected value of {@code widget}. If the given {@link ListBox}
	 * has multiple select enabled, we'll throw an {@link IllegalStateException}.
	 * 
	 * @param widget
	 *            the {@code ListBox} to get the value from.
	 * @param data
	 *            the new value for {@code widget}.
	 * @throws IllegalStateException
	 *             if the given {@link ListBox} has multiple select enabled.
	 */
	public void setData(ListBox widget, String data) {

		// we don't support multiple select.
		if (widget.isMultipleSelect()) {
			throw new IllegalStateException("Multiple select ListBoxes "
					+ "are currently not supported");
		}

		// go through the list of
		for (int i = 0; i < widget.getItemCount(); i++) {
			if (widget.getValue(i).equals(data)) {
				widget.setSelectedIndex(i);
				break;
			}
		}
	}

	/**
	 * Returns the default value that will be skipped if encountered.
	 * 
	 * @return the default value that will be skipped if encountered.
	 */
	public String getDefaultValue() {
		return defaultValue;
	}

	/**
	 * Sets the default value that will be skipped if encountered. An example of
	 * a default value would be "Choose X" which we wouldn't want returned.
	 * 
	 * @param defaultValue
	 *            the default value that will be skipped if encountered.
	 */
	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}

}
